package com.ac.api.group.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.helper.StringUtils;
import com.ac.api.group.dao.AcGrouptypeMapper;
import com.ac.api.group.entity.*;
import com.ac.api.group.service.*;
import com.ac.api.group.vo.GroupTypeActionVo;
import com.ac.api.group.vo.GroupTypeModuleVo;
import com.ac.api.group.vo.GroupTypeTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzl
 * @since 2020-07-09
 */
@Service
public class AcGrouptypeServiceImpl extends ServiceImpl<AcGrouptypeMapper, AcGrouptype> implements AcGrouptypeService {

    @Autowired
    private AcModuleService      acModuleService;
    @Autowired
    private AcActionService      acActionService;
    @Autowired
    private AcGroupService       acGroupService;
    @Autowired
    private AcGroupActionService acGroupActionService;

    @Override
    public List<AcGrouptype> list(AcGrouptype example) {
        return this.list(buildWrapper(example));
    }

    @Override
    public IPage<AcGrouptype> page(AcGrouptype example, IPage page) {
        return this.page(page, buildWrapper(example));
    }

    @Override
    public GroupTypeTreeVo findByGrouptype(Integer id) {
        AcGrouptype acGrouptype = this.getById(id);
        if (null == acGrouptype || "0".equals(acGrouptype.getState())) {
            throw new RuntimeException("没有找到id对应角色类型");
        }
        GroupTypeTreeVo tree      = new GroupTypeTreeVo(acGrouptype);
        List<AcModule>  acModules = acModuleService.findByGrouptypeid(id);
        if (null != acModules && acModules.size() > 0) {
            tree.setModules(acModules.stream().map(e -> {
                GroupTypeModuleVo module  = new GroupTypeModuleVo(e);
                List<AcAction>    actions = acActionService.findByModuleid(e.getId());
                if (null != actions && actions.size() > 0) {
                    module.setActions(actions.stream().map(GroupTypeActionVo::new).collect(Collectors.toList()));
                }
                return module;
            }).collect(Collectors.toList()));
        }
        return tree;
    }

    private QueryWrapper<AcGrouptype> buildWrapper(AcGrouptype example) {
        QueryWrapper<AcGrouptype> wrapper = new QueryWrapper<>(example);
        String                    name    = example.getName();
        wrapper.lambda()
                .like(StringUtils.isNotEmpty(name), AcGrouptype::getName, name)
                .eq(AcGrouptype::getState, "1")
                .orderByAsc(AcGrouptype::getId);
        return wrapper;
    }

    @Override
    public List<GroupTypeModuleVo> findGroupModule(Integer groupid) {
        AcGroup acGroup = acGroupService.getById(groupid);
        if (null == acGroup || "0".equals(acGroup.getState())) {
            throw new RuntimeException("没有找到id对应角色");
        }
        List<AcGroupAction> groupActions = acGroupActionService.findByGroupid(groupid);
        if (null == groupActions || groupActions.size() == 0) {
            return Collections.emptyList();
        }
        List<String> groupActionCodes =
                groupActions.stream().map(AcGroupAction::getAction).collect(Collectors.toList());
        List<AcModule> acModules = acModuleService.findByGrouptypeid(acGroup.getGrouptypeid());
        if (null != acModules && acModules.size() > 0) {
            List<GroupTypeModuleVo> groupTypeModules = new ArrayList<>();
            for (AcModule acModule : acModules) {
                List<AcAction> actions = acActionService.findByModuleid(acModule.getId());
                actions =
                        actions.stream().filter(e -> groupActionCodes.contains(e.getAction())).collect(Collectors.toList());
                if (null != actions && actions.size() > 0) {
                    GroupTypeModuleVo module = new GroupTypeModuleVo(acModule);
                    module.setActions(actions.stream().map(GroupTypeActionVo::new).collect(Collectors.toList()));
                    groupTypeModules.add(module);
                }
            }
            return groupTypeModules;
        }
        return Collections.emptyList();
    }

}
